/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @SearchAdapter.java  2014-9-10 下午3:34:28 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.act.ActBaLists;
import com.android.tiango.baba.entity.SubjectModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.view.RoundCornerImageView;

public class SearchAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<SubjectModel> adapterList = new ArrayList<SubjectModel>();
	private ImageLoader mLoader;

	public SearchAdapter(Context context) {
		this.context = context;
		mLoader = new ImageLoader(context, R.drawable.ic_default);
	}

	public void putData(ArrayList<SubjectModel> list, int page) {
		if (page == 1) {
			adapterList.clear();
			adapterList.addAll(list);
		} else {
			adapterList.addAll(list);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return adapterList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_search, null);
			holder.iv_pic = (RoundCornerImageView) convertView.findViewById(R.id.iv_pic);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final SubjectModel bean = adapterList.get(position);
		if (null != bean) {
			holder.tv_name.setText(bean.getSubjectName());
			holder.tv_content.setText(bean.getBars());
			mLoader.loadImage(bean.getSubjectImage(), holder.iv_pic);
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, ActBaLists.class);
					intent.putExtra(Constants.BARID, bean.getSubjectID());
					intent.putExtra(Constants.BARNAME, bean.getSubjectName());
					context.startActivity(intent);
				}
			});
		}
		return convertView;
	}

	class ViewHolder {
		private RoundCornerImageView iv_pic;
		private TextView tv_name;
		private TextView tv_content;

	}
}
